package org.txazo.stat.util.md5;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.MessageDigest;

/**
 * MD5文件加密工具类
 * 
 * @author Tu XiaoZhou
 * @since 2013-03-31
 */
public class FileMD5 {

	/**
	 * 文件MD5加密
	 */
	public static String md5(File file) {
		String cipherText = null;
		FileInputStream fis = null;
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			fis = new FileInputStream(file);
			byte[] buffer = new byte[1024];
			int length = -1;
			while ((length = fis.read(buffer)) != -1) {
				md.update(buffer, 0, length);
			}
			byte[] data = md.digest();
			cipherText = MD5.byteToHexString(data);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return cipherText;
	}

}
